<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    var arr = [1, 2, 3, 4];
    console.log(arr.join()); //1,2,3,4
    console.log(arr.join(":")); //1:2:3:4
    console.log(arr); //[1,2,3,4],原数组不变
</script>
<script>
    function repeatStr(str, n) {
        return new Array(n + 1).join(str);
    }
    console.log(repeatStr("嗨", 3)); //嗨嗨嗨
    console.log(repeatStr("Hi", 3)); //HiHiHi
    console.log(repeatStr(1, 3));    //111
</script>
<script>
    var arr = [1, 2, 3, 4]; //shift 
    var shift_arr = arr.shift();
    console.log(arr); // [2, 3, 4] 
    console.log(shift_arr); // 1 //unshift 
    var unshift_arr = arr.unshift("Tom");
    console.log(arr); // ["Tom", 2, 3, 4] 
    console.log(unshift_arr); // 
</script>
<script>
    var arr = [1, 100, 5, 20];
    console.log(arr.sort()); // [1, 100, 20, 5] 
    console.log(arr); // [1, 100, 20, 5] (原数组改变)
</script>
<script>
    var arr = [1, 100, 5, 20];
    function sortNumber(a, b) { return a - b };
    console.log(arr.sort(sortNumber)); //[1, 5, 20, 100] 
    console.log(arr); //[1, 5, 20, 100] (原数组改变)
</script>
<script>
    var arr = [1, 100, 5, 20];
    function sortNumber(a, b) { return b - a };
    console.log(arr.sort(sortNumber)); // [100, 20, 5, 1]
    console.log(arr); // [100, 20, 5, 1] (原数组改变) 
</script>
<script>
    var arr = [12, 25, 5, 20];
    console.log(arr.reverse()); // [20, 5, 25, 12] 
    console.log(arr); // [20, 5, 25, 12] (原数组改变)
</script>
<script>
    var arr = [1, 2, 3, 4];
    var arr2 = [11, 12, 13];
    var arrCopy = arr.concat(arr2);
    console.log(arr.concat()); // [1, 2, 3, 4] (复制数组) 
    console.log(arrCopy); // [1, 2, 3, 4, 11, 12, 13] 
    console.log(arr); // [1, 2, 3, 4] (原数组未改变)
</script>
<script>
    var arr = [1, 2, 3, 4];
    var arr2 = [11, [12, 13]]
    var arrCopy = arr.concat(arr2);
    console.log(arrCopy); // [1, 2, 3, 4, 11, Array(2)] 
    console.log(arr); // [1, 2, 3, 4] (原数组未改变)
</script>
<script>
    var arr = [1, 4, 6, 8, 12];
    var arrCopy1 = arr.slice(1);
    var arrCopy2 = arr.slice(0, 4);
    var arrCopy3 = arr.slice(1, -2);
    var arrCopy4 = arr.slice(-5, 4);
    var arrCopy5 = arr.slice(-4, -1)
    console.log(arrCopy1);  // [4, 6, 8, 12]
    console.log(arrCopy2);  // [1, 4, 6, 8] 
    console.log(arrCopy3);  // [4, 6] 
    console.log(arrCopy4);  // [1, 4, 6, 8]
    console.log(arrCopy5);  // [4, 6, 8]
    console.log(arr);  // [1, 4, 6, 8, 12] (原数组未改变) 
</script>
<script>
    var arr = ["张三", "李四", "王五", "小明", "小红"];
    /**************删除"王五"****************/
    var arrReplace1 = arr.splice(2, 1);
    console.log(arrReplace1);  // ["王五"] 
    console.log(arr);  // ["张三", "李四", "小明", "小红"] (原数组改变)
    //删除多个
    var arrReplace2 = arr.splice(1, 2);
    console.log(arrReplace2);  //  ["李四", "小明"] 
    console.log(arr);  // ["张三", "小红"]
    /**************添加"小刚"****************/
    var arrReplace3 = arr.splice(1, 0, "小刚");
    console.log(arrReplace3);  // [] (没有删除元素，所以返回的是空数组)
    console.log(arr);  // ["张三", "小刚", "小红"]
    //添加多个
    var arrReplace4 = arr.splice(3, 0, "刘一", "陈二", "赵六");
    console.log(arrReplace4);  // []
    console.log(arr);  // ["张三", "小刚", "小红", "刘一", "陈二", "赵六"]
    /**************"王五"替换"小刚"****************/
    var arrReplace5 = arr.splice(1, 1, "王五");
    console.log(arrReplace5);  // ["小刚"]
    console.log(arr);  // ["张三", "王五", "小红", "刘一", "陈二", "赵六"]
    //替换多个
    var arrReplace6 = arr.splice(1, 4, "李四");
    console.log(arrReplace6);  // ["王五", "小红", "刘一", "陈二"]
    console.log(arr);  // ["张三", "李四", "赵六"]
</script>


</html>